package com.lxmes;


import com.alibaba.fastjson.JSONObject;
import com.lxmes.common.utils.HttpClientUnit;
import com.lxmes.common.utils.WebServiceUnit;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        String lgoin = "<login sSubId='AS' sAccID='(default)@666' sYear='2024' sUserID='API' sPassword='1' sDate='2024-09-04' sServer='192.168.3.13' sSerial=''/>";
        Head head = new Head();
       // head.id = 1000000100;
       // head.ccode = "";
        head.ddate = "9-12-2024 00:00:00";
       // head.bomfirst = "1";
        head.cvenabbname = "济南森峰激光科技股份有限公司";
        head.iverifystate = "0";
        head.iswfcontrolled = "0";
        head.cmaker = "高岩";
        head.iexchrate = 1;
        head.cexch_name = "美元";
       // head.ufts = "AAAAAABpKWE=";
        head.bpufirst = "0";
        head.cvencode = "2202001";
        head.cvouchtype = "01";
        head.cwhcode = "01";
        head.brdflag =1;
        head.csource ="采购到货单" ;
        //head.iflowid = "0";
        //head.cflowname = "";
        head.cbustype = "普通采购";
       // head.chinvsn ="";
       // head.csysbarcode ="2202001";

        body body1 = new body();
       // body1.autoid = 1000000100;
        //body1.id =1000000100;
        body1.cinvcode = "A01060097";
        body1.cinvm_unit = "片";
        body1.iquantity = 4;
        body1.editprop = "A";
        body1.imatsettlestate = 0;


        String dom  = HttpClientUnit.doPostXml(head);
        String body = HttpClientUnit.doPostXml(body1);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            // 创建HttpPost实例
            HttpPost httpPost = new HttpPost("http://192.168.3.13:10000/u8api.asmx/PuStoreInAdd");

            // 设置请求头
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

            // 设置请求参数

            // 创建参数列表
            List<BasicNameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("login",lgoin));
            params.add(new BasicNameValuePair("DomHead", dom));
            params.add(new BasicNameValuePair("DomBody", body));
            params.add(new BasicNameValuePair("bCheck", "false"));
            params.add(new BasicNameValuePair("bBeforCheckStock","false"));
            params.add(new BasicNameValuePair("bIsRedVouch","false"));
            // 将参数列表转换为请求体
            StringEntity requestEntity = new UrlEncodedFormEntity(params,"UTF-8");
            httpPost.setEntity(requestEntity);

            // 发送POST请求
            CloseableHttpResponse response = httpClient.execute(httpPost);

            try {
                // 检查响应状态码
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == 200) {
                    // 获取响应体作为字符串
                    String result = EntityUtils.toString(response.getEntity(), "UTF-8");
                    JSONObject json =  WebServiceUnit.xml2Json(result);
                    System.out.println("Response: " + result);
                    System.out.println(json);
                } else {
                    System.err.println("Error: " + response.getStatusLine());
                }
            } finally {
                // 关闭响应
                response.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭HttpClient
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

}
    class Head {
        public int id;
        public String ddate;
        public String cvenabbname;
        public String iverifystate;
        public String iswfcontrolled;
        public String cmaker;
        public double iexchrate;
        public String cexch_name;

        public String bpufirst;
        public String cvencode;
        public String cvouchtype;
        public String cwhcode;
        public int brdflag;
        public String csource;
        public String iflowid;
        public String ufts;
        public String bomfirst;
        public String ccode;
        public String cflowname;
        public String csysbarcode;
        public String chinvsn;
        public String cbustype;
    }

class body{

    public String cinvcode;
    public String cinvm_unit;
    public double iquantity;
    public String editprop;
    public int imatsettlestate;
    public String irowno;

    public int autoid;

    public int id;
    public String creworkmocode;
    public String ireworkmodetailsid;
    public String iproducttype;
    public String cmaininvcode;
    public String imainmodetailsid;
    public String isharematerialfee;
    public String cinvouchtype;
    public String idebitids;
    public String imergecheckautoid;
    public String outcopiedquantity;
    public String ioldpartid;
    public String foldquantity;
    public String cbsysbarcode;
    public String cbmemo;
    public String iFaQty;
    public String istax;

    public String cbinvsn;
    public String strowguid;
    public String cplanlotcode;
    public String taskguid;
    public String bgift;



}